using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
	public Bow bow;

	public Weapon weapon;

	public AudioSource audioSource;

	public Rigidbody2D rigidBody;

	public Collider2D colliderBody;

	public TrailRenderer trail;

	public List<AudioClip> impactAudios;

	public bool damagable;

	[HideInInspector]
	public float penetration;

	private void Start()
	{
		weapon.penetration = bow.penetration + bow.penetration * Player.Instance.strength * bow.penetrationPlayerScaler;
		weapon.damage = bow.damage + bow.damage * Player.Instance.strength * bow.damagePlayerScaler;
		weapon.stoppingPower = bow.stoppingPower + bow.stoppingPower * Player.Instance.strength * bow.stoppingPowerPlayerScaler;
		penetration = weapon.penetration;
		bow.PlayReleaseStingAudio();
	}

	private void FixedUpdate()
	{
		if (Vector2.Distance(Player.Instance.transform.position, base.transform.position) > 50f)
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (!damagable || !collision.GetComponent<BodyPart>())
		{
			return;
		}
		RaycastHit2D[] array = Physics2D.RaycastAll(base.transform.position, base.transform.right, 1.5f, Ground.shootableLayerMask);
		if (array.Length == 0)
		{
			return;
		}
		RaycastHit2D[] array2 = array;
		for (int i = 0; i < array2.Length; i++)
		{
			RaycastHit2D raycastHit2D = array2[i];
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.tag == "Body"))
			{
				continue;
			}
			BodyPart component = raycastHit2D.collider.GetComponent<BodyPart>();
			if (!component)
			{
				continue;
			}
			component.OnHit(weapon, base.transform.right, raycastHit2D.point);
			penetration -= component.resistance;
			bool flag = component.name == "Shield" || component.name == "RiderHelmet" || component.name == "Gear";
			if (!flag && (bool)audioSource && audioSource.isActiveAndEnabled)
			{
				audioSource.PlayOneShot(impactAudios[Random.Range(0, impactAudios.Count)]);
			}
			if (penetration <= 0f)
			{
				if (flag)
				{
					colliderBody.isTrigger = false;
					rigidBody.velocity = Vector2.left * Random.value * 5f;
					rigidBody.AddTorque(30f + Random.value * 30f);
					base.gameObject.layer = component.gameObject.layer;
				}
				else
				{
					base.transform.SetParent(component.transform);
					Object.Destroy(rigidBody);
					Object.Destroy(colliderBody);
					Object.Destroy(this);
				}
				Object.Destroy(trail);
				damagable = false;
				break;
			}
		}
	}
}
